<?php
namespace common\models\favorites;

use common\models\goods\Goods;
use common\models\store\Store;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%favorites}}".
 *
 */
class Favorites extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%favorites}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id','fav_id','fav_type'], 'required'],
            [['member_id','fav_id','created_at','status'], 'integer'],
            [['fav_type'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            're_id' => '收藏ID',
            'member_id' => '用户ID',
            'fav_id'=>'收藏ID',
            'fav_type'=>'类型',
            'created_at' => '时间',
            'status'=>'状态'
        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    public function getGoods()
    {
        return $this->hasOne(Goods::className(),['goods_id'=>'fav_id'])
            ->select(['goods_id','goods_commonid','goods_name','goods_price','goods_marketprice','goods_serial','goods_salenum','goods_collect','goods_storage','goods_image','status']);
    }

    public function getStore()
    {
        return $this->hasOne(Store::className(),['id'=>'fav_id'])
            ->select(['id','name','logo','address_name','address_detail']);
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ];
    }
}
